package com.eshealthy.utils;

import java.util.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

public class Week {
    public int dateToWeek(String week) {
        int num=0;
        String[] weekDays = {"周日", "周一", "周二", "周三", "周四", "周五", "周六"}; //可以其他的表现形式
        for(int i=0;i<weekDays.length;i++){
            if(weekDays[i].equals(week))
            {  num=i+1;
                return num;
            }
        }
        return num;

    }
    /**
     * 获取上(下)周周几的日期
     * 值范围 {'SUNDAY': '星期日','MONDAY':'星期一','TUESDAY':'星期二','WEDNESDAY':'星期三','THURSDAY':'星期四','FRIDAY':'星期五','SATURDAY':'星期六'}
     * @param weekOffset  周偏移，上周为-1，本周为0，下周为1，以此类推
     */
    public static Date getDayOfWeek(int dayOfWeek, int weekOffset){
        Date dateNow = new Date();
//        System.out.println(dateNow);
        Calendar date = Calendar.getInstance(Locale.CHINA);
        //周数减一，即上周
        date.add(Calendar.WEEK_OF_MONTH,weekOffset);
        //日子设为周几
        date.set(Calendar.DAY_OF_WEEK, dayOfWeek);
        //时分秒全部置0
//        date.set(Calendar.DATE, 0);

        return date.getTime();
    }
    public int[] oneWeek(){
        String[] weekDay = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday","Sunday"}; //可以其他的表现形式
        int [] t = new int[7]; //可以其他的表现形式
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        boolean isFirstSunday = (calendar.getFirstDayOfWeek() == Calendar.SUNDAY);
        int weeks=0;
        for (int i=0;i<7;i++) {
            calendar.setTime(new Date()); // 把当前系统时间赋值给calendar
            calendar.add(calendar.DATE, i); // 在日期中增加天数,1天
            Date date = calendar.getTime();
            String dateStr = sdf.format(date);
            weeks= calendar.get(Calendar.DAY_OF_WEEK);
            if(isFirstSunday){
                weeks = weeks - 1;
                if(weeks == 0){
                    weeks = 7;
                }
            }
           t[i]=weeks;
//打印周几
            System.out.println(t[i]);
        }

        return t;
    }

}
